package leetcode.code002;

import leetcode.helper.tree.ListNode;

public class Solution extends Solution002 {

	@Override
	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		ListNode p = new ListNode();
		ListNode head = p;
		int carry = 0;
		while (l1 != null && l2 != null) {
			p.next = new ListNode();
			p = p.next;
			int num = l1.val + l2.val + carry;
			p.val = num % 10;
			carry = num / 10;
			l1 = l1.next;
			l2 = l2.next;
		}
		while (l1 != null) {
			p.next = new ListNode();
			p = p.next;
			int num = l1.val + carry;
			p.val = num % 10;
			carry = num / 10;
			l1 = l1.next;
		}
		while (l2 != null) {
			p.next = new ListNode();
			p = p.next;
			int num = l2.val + carry;
			p.val = num % 10;
			carry = num / 10;
			l2 = l2.next;
		}
		if (carry != 0) {
			p.next = new ListNode();
			p = p.next;
			p.val = carry;
		}
		return head.next;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
	}

}
